package com.tcy.game.angryrobots.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.tcy.game.angryrobots.AngryRobotsGame;
import com.tcy.game.angryrobots.Assets;
import com.tcy.game.angryrobots.general.CameraHelper;
import com.tcy.game.angryrobots.general.GameScreen;
import com.tcy.game.angryrobots.general.ScoreString;

/**
 * ScoresScreen
 * Created by 80002023 on 2016/7/27.
 */
public class ScoresScreen extends GameScreen<AngryRobotsGame> {

    private static final String LAST_SCORE = "Score:";
    private static final String TOP_SCORE = "Best: ";
    private static final String VERY_ANGRY_ROBOTS = "VeryAngryRobots";
    private static final String TOP_SCORE_PREF = "top score";
    private static final String TAP_TO_CONTINUE = "Tap to continue";

    private ScoreString scoreString;
    private ScoreString topScoreString;
    private OrthographicCamera scoreCam;
    private SpriteBatch spriteBatch;
    private boolean touched;
    private int topScore;
    private final Preferences preferences;
    private float stateTime;

    public ScoresScreen(AngryRobotsGame game) {
        super(game);
        scoreCam = CameraHelper.createCamera2(CameraHelper.ViewportMode.PIXEL_PERFECT, Assets.width, Assets.height, Assets.pixelDensity);
        spriteBatch = new SpriteBatch();
        spriteBatch.setProjectionMatrix(scoreCam.combined);
        scoreString = new ScoreString();
        topScoreString = new ScoreString();
        preferences = Gdx.app.getPreferences(VERY_ANGRY_ROBOTS);
        topScore = preferences.getInteger(TOP_SCORE_PREF, 1000);
        topScoreString.setScore(topScore);
        stateTime = 0.0f;
    }

    public void setScore(int score) {
        scoreString.setScore(score);
        if (score > topScore) {
            topScore = score;
            preferences.putInteger(TOP_SCORE_PREF, topScore);
            preferences.flush();
            topScoreString.setScore(score);
        }
    }

    @Override
    public void render(float delta) {
        stateTime += delta;
        if (Gdx.input.justTouched()) {
            touched = true;
        } else if (touched && !Gdx.input.isTouched()) {
            touched = false;
            //game.setScreen(game.mainMenuScreen);
        } else {
            Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            spriteBatch.begin();
            Assets.scoreFont.setColor(Color.WHITE);
            //TODO Bitmap Font bound width set
//            TextBounds b = Assets.scoreFont.getBounds(LAST_SCORE);

            float w = /*b.width +*/ Assets.scoreFont.getSpaceWidth();
//            b = Assets.scoreFont.getBounds(scoreString);
//            float w2 = b.width;
            float x = (Assets.width - (w/* + w2*/)) / 2.0f;
            float y = 3 * Assets.height / 4.0f;
            Assets.scoreFont.setColor(Color.WHITE);
            Assets.scoreFont.draw(spriteBatch, TOP_SCORE, x, y);
            Assets.scoreFont.draw(spriteBatch, topScoreString, x + w, y);

//            b = Assets.scoreFont.getBounds(TOP_SCORE);
            w = /*b.width + */Assets.scoreFont.getSpaceWidth();
//            b = Assets.scoreFont.getBounds(topScoreString);
//            w2 = b.width;
            x = (Assets.width - (w/* + w2*/)) / 2.0f;
//            y += 2 * b.height;
            Assets.scoreFont.draw(spriteBatch, LAST_SCORE, x, y);
            Assets.scoreFont.draw(spriteBatch, scoreString, x + w, y);

            if (stateTime % 1.0f < 0.5f) {
                Assets.textFont.setColor(Color.WHITE);
//                Assets.textFont.drawWrapped(spriteBatch, TAP_TO_CONTINUE, 0, Assets.height / 4, Assets.width, HAlignment.CENTER);
                Assets.textFont.draw(spriteBatch, TAP_TO_CONTINUE, Assets.height / 4, Assets.width);
            }
            spriteBatch.end();
        }
    }

    @Override
    public void show() {
        Gdx.input.setCatchBackKey(true);
        stateTime = 0.0f;
    }

    @Override
    public void hide() {
        Gdx.input.setCatchBackKey(false);
    }

    @Override
    public void resume() {
        Gdx.input.setCatchBackKey(true);
    }
}
